package fr.u.psud.polytech.view;

import java.util.Iterator;
import java.util.Set;

import fr.u.psud.polytech.controller.ActionManager;
import fr.u.psud.polytech.controller.UserManager;
import fr.u.psud.polytech.database.Action;
import fr.u.psud.polytech.database.User;
import fr.u.psud.polytech.view.ressources.ActionType;

public class PasswordChecker {
	
	public boolean checkUser(String login, String password, ActionType type)
	{
		boolean ret = false;
		
		User u = new UserManager().getUser(login);
		Boolean b = false;
		System.out.println(type.getUrl());

		Set <Action> listAction = u.getListAction();
		Iterator<Action> it = listAction.iterator();
		
		while(it.hasNext()){
			Action e = it.next();
			System.out.println("iterator : "+e.getUrl());
			if(e.getUrl().compareTo(type.getUrl())==0){
				b = true;
			}
		}
		
		
		//Verification du mot de passe de l'utilisateur et verification qu'il a le droit
		//d'ouvrir la page WEB
		if(u != null && u.getPassword().compareTo(password) == 0 && b){
				System.out.println("first passed");
			
				ret = true;
			
		}
		else if(u != null && u.getPassword().compareTo(password)==0){
			System.out.println("second passed");
			if(type == ActionType.NOACTIONRIGHT){
				System.out.println("third passed");
				ret = true;	
			}
		}
			
		
		
		return ret;
	}
}
